package com.liu.qqRobot.climbingTower.runnable;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class AttackRunnable implements Runnable {

    private final CountDownLatch countDownLatch;

    private final AtomicInteger agile;

    private final AtomicInteger hp;

    private final AtomicInteger aggressivity;

    private String attacker;

    private String byAttacker;

    private List<String> msgList;

    public AttackRunnable(CountDownLatch countDownLatch, AtomicInteger agile, AtomicInteger hp, AtomicInteger aggressivity,
                          String attacker, String byAttacker, List<String> msgList) {
        this.countDownLatch = countDownLatch;
        this.agile = agile;
        this.hp = hp;
        this.aggressivity = aggressivity;
        this.attacker = attacker;
        this.byAttacker = byAttacker;
        this.msgList = msgList;
    }

    @Override
    public void run() {
        int oldHp = hp.get();
        agile.incrementAndGet();
        if (agile.get() >= 20) {
            int newHp = hp.get() - aggressivity.get();
            hp.set(newHp);
            if (attacker.equals("用户")) {
                msgList.add("<div style=\"color: #64ab6f\">【" + attacker + "】攻击" + byAttacker + "造成伤害：" + Math.min(aggressivity.get(), oldHp) + "</div>");
                msgList.add("<div style=\"color: #64ab6f\">" + byAttacker + "生命值：" + oldHp + "→" + Math.max(newHp, 0) + "</div>");
            } else {
                msgList.add("<div style=\"color: #ee6161\">【" + attacker + "】攻击" + byAttacker + "造成伤害：" + Math.min(aggressivity.get(), oldHp) + "</div>");
                msgList.add("<div style=\"color: #ee6161\">" + byAttacker + "生命值：" + oldHp + "→" + Math.max(newHp, 0) + "</div>");
            }
        }
        countDownLatch.countDown();
    }

}
